/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.rocketmq.console.config;

import java.io.File;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.MixAll;
import org.apache.rocketmq.console.script.model.EnvironDic;
import org.apache.rocketmq.console.script.service.EnvService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import static org.apache.rocketmq.client.ClientConfig.SEND_MESSAGE_WITH_VIP_CHANNEL_PROPERTY;

@Configuration
@ConfigurationProperties(prefix = "rocketmq.config")
public class RMQConfigure {
	@Autowired
	public EnvService envService;
	private Logger logger = LoggerFactory.getLogger(RMQConfigure.class);
	// use rocketmq.namesrv.addr first,if it is empty,than use system proerty or
	// system env
	private volatile String namesrvAddr = System.getProperty(MixAll.NAMESRV_ADDR_PROPERTY,
			System.getenv(MixAll.NAMESRV_ADDR_ENV));

	private volatile String isVIPChannel = System.getProperty(SEND_MESSAGE_WITH_VIP_CHANNEL_PROPERTY, "true");

	private String dataPath;
	private String clusterName;
	private boolean enableDashBoardCollect;

	public String getNamesrvAddr() {
		return namesrvAddr;
	}

	public void setNamesrvAddr(String namesrvAddr) {
		if (StringUtils.isNotBlank(namesrvAddr)) {
			this.namesrvAddr = namesrvAddr;
			System.setProperty(MixAll.NAMESRV_ADDR_PROPERTY, namesrvAddr);
			logger.info("setNameSrvAddrByProperty nameSrvAddr={}", namesrvAddr);
		}
	}

	public String getClusterName() {
		return clusterName;
	}

	public void setClusterName(String clusterName) {
		this.clusterName = clusterName;
	}

	public String getRocketMqConsoleDataPath() {
		return dataPath;
	}

	public String getConsoleCollectData() {
		return dataPath + File.separator + "dashboard";
	}

	public void setDataPath(String dataPath) {
		this.dataPath = dataPath;
	}

	public String getIsVIPChannel() {
		return isVIPChannel;
	}

	public void setIsVIPChannel(String isVIPChannel) {
		if (StringUtils.isNotBlank(isVIPChannel)) {
			this.isVIPChannel = isVIPChannel;
			System.setProperty(SEND_MESSAGE_WITH_VIP_CHANNEL_PROPERTY, isVIPChannel);
			logger.info("setIsVIPChannel isVIPChannel={}", isVIPChannel);
		}
	}

	public boolean isEnableDashBoardCollect() {
		return enableDashBoardCollect;
	}

	public void setEnableDashBoardCollect(String enableDashBoardCollect) {
		this.enableDashBoardCollect = Boolean.valueOf(enableDashBoardCollect);
	}

	public EnvironDic getCurrentRegion() {
		EnvironDic environDic = null;
		String[] namesrvAddrList = namesrvAddr.split(";");
		for (String nameserverAddr : namesrvAddrList) {
			environDic = envService.findOneByNameserveLike(nameserverAddr);
			if (environDic != null) {
				break;
			}
		}
		return environDic;
	}
}
